<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Contains the Members display component
 *  This should not be triggered independently, but as a component of another page.
 */


/**
 *  Display the members information page
 *  @param $a_allowEdit If true, we display an edit version of the component
 *  @param $showID      ID of the show to display
 *  @param $model       The show model
 *  @param $showData    data pertaining this show
 */
function displayMembers( $a_alowEdit = false, $showID, &$model, &$showData )
{
    // The set of members who have responded, and those who haven't
    $knownMembers   = $model->getKnownMembers($showID);
    $unknownMembers = $model->getUnknownMembers($showID);
    $showRoles      = $model->getShowRoles();
    
    // Protect against zero members
    if(!is_array($knownMembers)){
        $knownMembers = array();
    }
    
    $responded = count($knownMembers);
    $absent    = count($unknownMembers);
    
    // Arrays for counting status and roles. Status is a progressive index
    // array, but roles are not necessarily so, as they are grouped by id
    $statusCount = array_fill( 0, count($model->_userStatusLabels), 0 );
    $roleCount   = array();
    
    // Initialize show roles
    foreach( $showRoles as $role ) {
        $roleCount[ $role['id'] ] = 0;
    }
    ?>
    <script language="javascript" type="text/javascript">
    <!--
    
        _jq(function(){
            var theCheckbox = _jq('#members-participate');
            theCheckbox.click(function(){
                if (theCheckbox.attr('checked')){
                    _jq('#myParticipation').fadeIn(GUI_FADE_SPEED);
                } else {
                    _jq('#myParticipation').fadeOut(GUI_FADE_SPEED);
                    
                    // Remove my participation now
                    // ...
                }
            })
        });
    
    -->
    </script>
    
    <a name="members" style="text-decoration:none;">
    <section id='show-data'>
        <div id='section-title'>
            <h1>Members</h1>
        </div>
        <form style="height: 2em; margin: 0 0 0 6em; padding:0;">
            <input type="checkbox" id='members-participate'>I wish to participate in this show</fieldset>
        </form>
        <div id="myParticipation" style="display:none;">
            <form class="member-entry" style="margin-top:0;" method='post' action="<?php echo JRoute::_('index.php?');?>">
                <ol>
                    <li>
                        <label for="role" class = "mp-label" >My role is</label>
                        <select id="mp-role" name="role" class="mp-select">
                            <?php
                                foreach( $showRoles as $role )
                                {
                                    echo '<option value="'.$role['id'].'">'.$role['name'].'</option>';
                                }
                            ?>
                        </select>
                    </li>
                    <li>
                        <label for="status" class="mp-label">and my status is</label>
                        <select id="mp-status" name="status" class="mp-select">
                            <?php
                            echo $model->getUserStatusHTMLCombo();
                            ?>
                        </select>
                    </li>
                    <li>
                        <label for="notify" class="mp-label">Apply and Notify</label>
                        <select name='notify'>
                            <?php echo $model->getNotificationTargetsHTMLCombo(); ?>
                        </select>                                            
                        <input type="submit" value="now" />
                        <input type="hidden" name="option"     value="com_musashi" />
                        <input type="hidden" name="task"       value="updateParticipation" />
                        <input type="hidden" name="controller" value="shows" />
                        <input type="hidden" name="sid"        value="<?php echo $showID; ?>" />
                        <input type="hidden" name="mid"        value="<?php echo $model->getMemberID(); ?>" />                                                         
                    </li>
                </ol>
            </form>
        </div>
        <div id="member-container">
            <div id="accounted">
                <!--
                <form class="member-entry" style="background-color:#DADADA;">
                    <h2>Someone</h2>
                    <select>
                        <option>confirmed</option>
                        <option>unsure</option>
                        <option>abscent</option>
                    </select>
                    <select>
                        <option>floorplanner</option>
                        <option>player</option>
                        <option>organizer</option>
                    </select>
                    <input type="button" value="Remove" />
                </form>
            -->
                <?php                    
                if( $responded > 0 )
                {
                    foreach ( $knownMembers as $accounted )
                    {
                        // Obtain this member's participation
                        $memberRoles = $model->getMemberRolesForShow( $accounted['id'], $showID );
                        
                        // One more sanity test
                        if( is_array($memberRoles) )
                        {
                            echo '<div class="member-entry">';
                            echo '<h2>'.$accounted['name'].'</h2>';
                            
                            // One entry per role
                            foreach( $memberRoles as $role )
                            {
                                // Count, then display
                                $statusCount[ $role['status'] ]++;
                                $roleCount[ $role['idRole'] ]++;
                                
                                echo '<form>';
                                echo '<select>';
                                echo $model->getUserStatusHTMLCombo( $role['status'] );
                                echo '</select>';
                                echo '<select>';
                                foreach( $showRoles as $roleOption )
                                {
                                    if ( $roleOption['id'] == $role['idRole'] ){
                                        echo '<option value="'.$roleOption['id'].'" SELECTED>'.$roleOption['name'].'</option>';
                                    } else {
                                        echo '<option value="'.$roleOption['id'].'" >'.$roleOption['name'].'</option>';
                                    }
                                }
                                echo '</select>';
                                echo '<input type="button" value="Remove" />';
                                echo '</form>';                                        
                            }
                            
                            echo '</div>';                                
                        }                            
                    }                        
                }
                else
                {
                    echo '<h2 style="width: 90%; margin:7em 0 0 2em; ">No members registered yet</h2>';
                }
                ?>
            </div>
            <div id="unaccounted">
                <?php
                foreach ( $unknownMembers as $unaccounted )
                {
                    echo '<form class="member-entry" id="member-'.$unaccounted['id'].'" >';
                    echo '<h2>'.$unaccounted['name'].'</h2>';
                    echo '</form>';
                }
                ?>
            </div>
        </div>
        <div id="member-stats">
            <div id="summary" class="member-stats-panel" style=" border-right:1px solid #cacaca;" >
                <ol>
                    <li>
                        <h2>Responded</h2><h3><?php echo $responded; ?></h3>
                    </li>
                    <li>
                        <h2>Absent</h2><h3><?php echo $absent; ?></h3>
                    </li>
                </ol>                    
            </div>
            <div id="particulars" class="member-stats-panel"  style="margin-left:10px; border-right:1px solid #cacaca;">
                <ol>
                <?php
                foreach( $model->_userStatusLabels as $index => $label )
                {
                    echo '<li><h2>'.$label.'</h2><h3>'.$statusCount[$index].'</h3></li>';
                }
                ?>
                </ol>
            </div>
            <div id="particulars" class="member-stats-panel"  style="margin-left:10px;">
                <ol>
                <?php
                foreach( $showRoles as $role )
                {
                    echo '<li><h2>'.$role['name'].'</h2><h3>'.$roleCount[ $role['id'] ].'</h3></li>';                        
                }
                ?>
                </ol>
            </div>
        </div>
    </section>
    </a>
    <?php        
}
?>
